package com.test.pudamoney.activity_fragment_pudamoney.info_pudamoney;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.test.pudamoney.R;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseInfoActivity_pudamoney;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessData_pudamoney;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessStep_pudamoney;
import com.test.pudamoney.constant_pudamoney.Constant_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney;
import com.test.pudamoney.view_pudamoney.base_pudamoney.BaseInfoInputViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.AddressPickViewManager_Niri_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.CreditReportViewManager_Niri_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.DatePickViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.GenderSelectViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.InputViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.info_input_pudamoney.SelectViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.widget_pudamoney.TitleView_pudamoney;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.TreeMap;

public class InfoUploadActivity_pudamoney extends BaseInfoActivity_pudamoney {

    private LinearLayout groupLayout;

    private ArrayList<BaseInfoInputViewManager_pudamoney> infoInputViewManagers = new ArrayList<>();

    private ProcessStep_pudamoney step;

    private String buriedPointFalg = "";
    private String activityFlag = "";

    //尼日银行选择
    private String paramNameBankCode = "";
    private ProcessData_pudamoney bankNameData;
    private final TreeMap<String,String> map_bankName_bankCode = new TreeMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_upload_pudamoney);
        init();
    }

    @SuppressLint("InflateParams")
    private void init(){
        TitleView_pudamoney titleView = findViewById(R.id.activity_info_upload_view_title);
        setBackBtn_pudamoney(titleView.getBackIb());

        groupLayout = findViewById(R.id.activity_info_upload_layout_group);

        findViewById(R.id.activity_info_upload_btn_next).setOnClickListener(v -> {
            for (BaseInfoInputViewManager_pudamoney widgetsManager: infoInputViewManagers){
                if (!widgetsManager.checkInput()){
                    showToast_pudamoney(getString(R.string.word_input_tip));
                    return;
                }
            }
            HttpManager_pudamoney.getInstance().saveUserBuriedPoint((buriedPointFalg + "_CLICK").toUpperCase());
            if (step ==null) return;
            String url = step.getApiUrl();
            if (url.isEmpty()) return;
            TreeMap<String,String> dataMap = new TreeMap<>();

            ArrayList<String> relashipList = new ArrayList<>();
            ArrayList<String> nameList = new ArrayList<>();
            ArrayList<String> phoneList = new ArrayList<>();

            for (BaseInfoInputViewManager_pudamoney baseInfoInputViewManager: infoInputViewManagers){

                ProcessData_pudamoney data = baseInfoInputViewManager.getData();
                if (data==null) continue;
                switch (data.getAction()){

                    case Constant_pudamoney.Data_Action_Relation:
                        relashipList.add(((SelectViewManager_pudamoney)baseInfoInputViewManager).getTextInput());
                        break;

                    case Constant_pudamoney.Data_Action_Name:
                        nameList.add(baseInfoInputViewManager.getInput());
                        break;

                    case Constant_pudamoney.Data_Action_Phone:
                        phoneList.add(baseInfoInputViewManager.getInput());
                        break;

                    case Constant_pudamoney.Data_Action_Address_Pick://地址联动模块
                        //尼日
                        if (!(baseInfoInputViewManager instanceof AddressPickViewManager_Niri_pudamoney)) continue;
                        AddressPickViewManager_Niri_pudamoney addressPickViewManager = (AddressPickViewManager_Niri_pudamoney) baseInfoInputViewManager;
                        dataMap.put(data.getParamNameState(),addressPickViewManager.getState());
                        dataMap.put(data.getParamNameCity(),addressPickViewManager.getCity());
                        dataMap.put(data.getParamNameArea(),addressPickViewManager.getArea());
                        break;

                    case Constant_pudamoney.Data_Action_Bank_Pick://尼日银行选择模块
                        String bankName = ((SelectViewManager_pudamoney) baseInfoInputViewManager).getTextInput();
                        String bankCode = map_bankName_bankCode.get(bankName);
                        dataMap.put(data.getParamNameBankName(),bankName);
                        dataMap.put(data.getParamNameBankCode(),bankCode);
                        break;


                    default: dataMap.put(data.getParamName(),baseInfoInputViewManager.getInput());
                }

            }

            //添加联系人信息
            if (!TextUtils.isEmpty(step.getContactParamName())){
                JSONArray jsonArray = new JSONArray();
                for (int i=0;i<relashipList.size();i++){
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("relation",relashipList.get(i));
                        if (i<nameList.size()) jsonObject.put("fullName",nameList.get(i));
                        if (i<phoneList.size()) jsonObject.put("mobile",phoneList.get(i));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    jsonArray.put(jsonObject);
                }
                dataMap.put(step.getContactParamName(),jsonArray.toString());
            }

            HttpManager_pudamoney.getInstance().uploadInfo(url,dataMap);
        });

        step = ProcessInfo_pudamoney.getInstance().getStep(0);
        String stepName = ProcessInfo_pudamoney.getInstance().getStepName(0);
        buriedPointFalg = stepName;
        switch (buriedPointFalg){
            case "personal_info":
                activityFlag = "BASIC";
                break;
            case "work_info":
                activityFlag = "WORK";
                break;
            case "contact_info":
                activityFlag = "CONTACT";
                break;
            case "account_info":
                activityFlag = "BVN";
                break;
            default:
                activityFlag = buriedPointFalg;
        }

        if (step ==null) return;

        titleView.setTitle(StringUtil_pudamoney.processTitleFormat(ProcessInfo_pudamoney.getInstance().getStepName(0)));

        //联系人信息单独处理
        if (step.isContact()){

            for (int i = 0; i< step.getExtDataListSize(); i++){
                ProcessData_pudamoney data = step.getExtProcessData(i);
                if (data==null) continue;
                addInputView(data,null);
            }

            //添加分割线
            if (step.getExtDataListSize()>0) groupLayout.addView(LayoutInflater.from(this).inflate(R.layout.view_line_pudamoney,null));

            int count = 0;

            while (count< step.getItemCount()){//联系人数量

                for (int i = 0; i< step.getDataListSize(); i++){
                    ProcessData_pudamoney data = step.getProcessData(i);
                    if (data==null) continue;
                    addInputView(data, step.getRelationShips(count));
                }

                //添加分割线
                if (++count< step.getItemCount()) groupLayout.addView(LayoutInflater.from(this).inflate(R.layout.view_line_pudamoney,null));

            }

            return;
        }

        for (int i = 0; i< step.getDataListSize(); i++){
            ProcessData_pudamoney data = step.getProcessData(i);
            if (data==null) continue;
            addInputView(data,null);
        }
    }

    //添加信息输入控件
    @SuppressLint("InflateParams")
    private void addInputView(ProcessData_pudamoney data, ArrayList<String> relationships){

        View inputView = null;

        switch (data.getAction()){

            case Constant_pudamoney.Data_Action_Select:
                inputView = LayoutInflater.from(this).inflate(R.layout.view_select_pudamoney,null);
                SelectViewManager_pudamoney selectViewManager = new SelectViewManager_pudamoney(inputView,data);
                infoInputViewManagers.add(selectViewManager);
                break;

            case Constant_pudamoney.Data_Action_Date_Pick:
                inputView = LayoutInflater.from(this).inflate(R.layout.view_date_picker_pudamoney,null);
                DatePickViewManager_pudamoney datePickWidgetsManager = new DatePickViewManager_pudamoney(inputView,data);
                infoInputViewManagers.add(datePickWidgetsManager);
                break;

            case Constant_pudamoney.Data_Action_Relation://联系人-关系
                if (relationships!=null) data.setRelationships(relationships);
                inputView = LayoutInflater.from(this).inflate(R.layout.view_select_pudamoney,null);
                SelectViewManager_pudamoney contactSelectViewManager = new SelectViewManager_pudamoney(inputView,data);
                infoInputViewManagers.add(contactSelectViewManager);
                break;

            case Constant_pudamoney.Data_Action_Gender_Pick:
                inputView = LayoutInflater.from(this).inflate(R.layout.view_gender_select_pudamoney,null);
                GenderSelectViewManager_pudamoney genderSelectViewManager = new GenderSelectViewManager_pudamoney(inputView,data);
                infoInputViewManagers.add(genderSelectViewManager);
                break;

            case Constant_pudamoney.Data_Action_Credit_Report://尼日同意征信收集模块
                inputView = LayoutInflater.from(this).inflate(R.layout.view_credit_report_niri_pudamoney,null);
                CreditReportViewManager_Niri_pudamoney creditReportViewManager = new CreditReportViewManager_Niri_pudamoney(inputView,data);
                infoInputViewManagers.add(creditReportViewManager);
                break;

            case Constant_pudamoney.Data_Action_Address_Pick://地址联动模块
                //尼日
                inputView = LayoutInflater.from(this).inflate(R.layout.view_address_pick_pudamoney,null);
                AddressPickViewManager_Niri_pudamoney addressPickViewManager = new AddressPickViewManager_Niri_pudamoney(inputView,data);
                infoInputViewManagers.add(addressPickViewManager);
                break;

            case Constant_pudamoney.Data_Action_Bank_Pick://尼日银行选择模块
                bankNameData = data;
                HttpManager_pudamoney.getInstance().fetchBankCodes_Niri();
                break;

            case Constant_pudamoney.Data_Action_Input:
            case Constant_pudamoney.Data_Action_Name://联系人-名字
            case Constant_pudamoney.Data_Action_Phone://联系人-电话
            default:
                inputView = LayoutInflater.from(this).inflate(R.layout.view_input_pudamoney,null);
                InputViewManager_pudamoney inputWidgetsManager = new InputViewManager_pudamoney(inputView,data);
                infoInputViewManagers.add(inputWidgetsManager);
                break;
        }

        if (inputView!=null) groupLayout.addView(inputView);
    }

    @Override
    public void recordPageResumeEvent_pudamoney() {
        HttpManager_pudamoney.getInstance().saveUserBuriedPoint((buriedPointFalg + "_START").toUpperCase());
    }

    @Override
    public void recordPagePauseEvent_pudamoney() {
        HttpManager_pudamoney.getInstance().saveUserBuriedPoint((buriedPointFalg + "_END").toUpperCase());
    }

    //获取银行列表响应（尼日）
    public void onFetchBankCodeResponse(JSONArray jsonArray){

        if (bankNameData == null || jsonArray == null) return;

        map_bankName_bankCode.clear();
        String[] bankNames = new String[jsonArray.length()];

        for (int i = 0;i<jsonArray.length();i++){
            JSONObject jsonObject = jsonArray.optJSONObject(i);
            String bankName = jsonObject.optString("bankName");
            String bankCode = jsonObject.optString("bankCode");
            bankNames[i] = bankName;
            map_bankName_bankCode.put(bankName,bankCode);
        }

        bankNameData.setBankNames(bankNames);
        @SuppressLint("InflateParams") View inputView = LayoutInflater.from(this).inflate(R.layout.view_select_pudamoney,null);
        SelectViewManager_pudamoney selectViewManager = new SelectViewManager_pudamoney(inputView, bankNameData);
        infoInputViewManagers.add(selectViewManager);

        groupLayout.addView(inputView);
    }

}